package org.argeproje.resim.property;

import java.lang.reflect.InvocationTargetException;

import org.argeproje.resim.ui.parts.CellDoubleValidator;
import org.eclipse.jface.viewers.ICellEditorValidator;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

public class DoublePRP extends Property{

	public DoublePRP(Object owner, String propertyId,
			String displayName, String getterMethod, String setterMethod) {
		super(owner, propertyId, displayName, getterMethod, setterMethod);
		try {
			_setterMethod = _owner.getClass().getMethod(setterMethod,Double.TYPE);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Object getValue() {
		try {
			return ((Double)_getterMethod.invoke(_owner)).toString();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void setValue(Object value) {
		
		try {
			_setterMethod.invoke(_owner,new Double(Double.parseDouble(value.toString())));
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public IPropertyDescriptor getDescriptor() {
		ICellEditorValidator lValidator = new CellDoubleValidator();
	    TextPropertyDescriptor lPropDesc = new TextPropertyDescriptor(_id,_displayName);
	    lPropDesc.setValidator(lValidator);
		return lPropDesc;
	}

}
